const Router = require('koa-router')
const {
    verifyUser,
    userValidator,
    bcryptPassword,
    verifyLogin
} = require('../middleware/user.middleware');
const {
    register,
    login,
    getUserList,
    getCurrentUserInfo,
    layout,
    getUserById,
    updateUser,
    deleteUser,
    updateUserStatus,
    batchDeleteUser
} = require('../controller/userController');
const {SmCode} = require('../config/smcode');
const {auth} = require("../middleware/auth.middleware");

const userRouter = new Router({prefix: '/user'})

//登录路由
userRouter.post('/login', userValidator, verifyLogin, login);

//退出登录路由
userRouter.post('/layout', auth, layout);

//注册路由
userRouter.post('/register', auth, userValidator, verifyUser, bcryptPassword, register);

//发送短信验证码
userRouter.post('/smcode', async ctx => {
    const {username} = ctx.request.body;
    await new SmCode(ctx, username).codeFun();
})

userRouter.post('/getSMcodeDetail', async ctx => {
    const {username, codeData} = ctx.request.body;
    await new SmCode(ctx, username, codeData).verificationCode();
})

//获取用户列表
userRouter.get('/list', auth, getUserList)

//获取用户列表
userRouter.get('/getDetail/:id', auth, getUserById)

//修改用户
userRouter.put('/update/:id', auth, updateUser)

//删除用户
userRouter.delete('/delete/:id', auth, deleteUser)

//批量删除用户
userRouter.post('/batchDelete', auth, batchDeleteUser)

//修改用户状态
userRouter.put('/updateStatus/:id', auth, updateUserStatus)

//获取用户信息
userRouter.get('/getCurrentUserInfo', auth, getCurrentUserInfo)

module.exports = userRouter
